package com.shanghaionstar.user.service;

import android.content.Context;

import com.onstar.cn.api.PublicService;
import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.util.Constants;
import com.onstar.cn.common.util.DeviceUtil;

import com.onstar.cn.framework.annotation.http.HeadsValue;
import com.onstar.cn.framework.annotation.http.Request;
import com.onstar.cn.framework.mvp.base.core.BaseService;
import com.onstar.cn.framework.network.HttpInjectUtils;
import com.onstar.cn.framework.network.entity.RequestEntity;
import com.onstar.cn.framework.network.enumtype.HttpAccept;
import com.onstar.cn.framework.network.enumtype.HttpMethod;
import com.onstar.cn.framework.network.enumtype.Language;
import com.onstar.cn.framework.utils.GsonHelper;
import com.onstar.cn.mag.enumtype.CategoryType;
import com.onstar.cn.mag.enumtype.OSType;
import com.onstar.cn.more.response.GetAllContentListResponseType;
import com.onstar.cn.more.response.GetContentListResponseType;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.utils.NetConnectUtil;

import rx.Observable;
import rx.functions.Func0;

/**
 * Created by 7828wc on 16/7/1.
 */
public class BuickCareService extends BaseService<GetAllContentListResponseType> {
    private Context context;

    public BuickCareService(Context context) {
        this.context = context;
    }

    @Override
    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.GET)
    public RequestEntity getRequestEntity() {
        return null;
    }

    @Override
    public GetAllContentListResponseType jsonToBean(String json) {
        return null;
    }

    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.GET)
    public String getCategory(CategoryType categoryType) throws OnStarException {
        PublicService service = new PublicService(Constants.CURRENT_SERVER_URL);
        RequestEntity entity = NetConnectUtil.getAuthRequestEntity(context);
        int versionCode =new DeviceUtil(context).getAppVersionCode();
        entity = service.contents(entity, OSType.ANDROID_SMALL.toString(), categoryType != null ? categoryType.value()
                : "",versionCode+"");
        entity = HttpInjectUtils.injectHttp(BuickCareService.this.getClass(), "getCategory", entity, CategoryType.class);
        return BusinessUtil.getResponse(getDatas(entity), context);
    }

    public Observable<GetContentListResponseType> getContentList(final CategoryType categoryType) {
        return Observable.defer(new Func0<Observable<GetContentListResponseType>>() {
            @Override
            public Observable<GetContentListResponseType> call() {
                try {
                    return Observable.just( GsonHelper.fromJson( getCategory(categoryType),GetContentListResponseType.class));
                } catch (OnStarException e) {
                    return Observable.error(e);
                }
            }
        });
    }

}
